package com.ruoyi.gis.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.aliyun.aiearth_engine20220609.models.ListDatasResponseBody;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.gis.domain.DatasetRasterBands;
import com.ruoyi.gis.domain.DatasetType;
import com.ruoyi.gis.domain.DatasetTypeBand;
import com.ruoyi.gis.service.IDatasetRasterBandsService;
import com.ruoyi.gis.service.IDatasetTypeBandService;
import com.ruoyi.gis.service.IDatasetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gis.mapper.DatasetRasterMapper;
import com.ruoyi.gis.domain.DatasetRaster;
import com.ruoyi.gis.service.IDatasetRasterService;

/**
 * 栅格数据Service业务层处理
 *
 * @author rww
 * @date 2024-08-30
 */
@Service
public class DatasetRasterServiceImpl implements IDatasetRasterService
{
    @Autowired
    private DatasetRasterMapper datasetRasterMapper;
    @Autowired
    private IDatasetRasterBandsService datasetRasterBandsService;
    @Autowired
    private IDatasetTypeService datasetTypeService;
    @Autowired
    private IDatasetTypeBandService datasetTypeBandService;
    /**
     * 查询栅格数据
     *
     * @param id 栅格数据主键
     * @return 栅格数据
     */
    @Override
    public DatasetRaster selectDatasetRasterById(Long id)
    {
        return datasetRasterMapper.selectDatasetRasterById(id);
    }

    /**
     * 查询栅格数据列表
     *
     * @param datasetRaster 栅格数据
     * @return 栅格数据
     */
    @Override
    public List<DatasetRaster> selectDatasetRasterList(DatasetRaster datasetRaster)
    {
        return datasetRasterMapper.selectDatasetRasterList(datasetRaster);
    }

    /**
     * 新增栅格数据
     *
     * @param datasetRaster 栅格数据
     * @return 结果
     */
    @Override
    public int insertDatasetRaster(DatasetRaster datasetRaster)
    {
        datasetRaster.setId(IdUtil.getSnowflakeNextId());
        return datasetRasterMapper.insertDatasetRaster(datasetRaster);
    }

    /**
     * 修改栅格数据
     *
     * @param datasetRaster 栅格数据
     * @return 结果
     */
    @Override
    public int updateDatasetRaster(DatasetRaster datasetRaster)
    {
        return datasetRasterMapper.updateDatasetRaster(datasetRaster);
    }

    /**
     * 批量删除栅格数据
     *
     * @param ids 需要删除的栅格数据主键
     * @return 结果
     */
    @Override
    public int deleteDatasetRasterByIds(Long[] ids)
    {
        return datasetRasterMapper.deleteDatasetRasterByIds(ids);
    }

    /**
     * 删除栅格数据信息
     *
     * @param id 栅格数据主键
     * @return 结果
     */
    @Override
    public int deleteDatasetRasterById(Long id)
    {
        return datasetRasterMapper.deleteDatasetRasterById(id);
    }
    @Override
    public  DatasetRaster selectDatasetRasterByDataId(String dataId) {
        return datasetRasterMapper.selectDatasetRasterByDataId(dataId);
    }
    @Override
    public void addDatasetRaster(List<ListDatasResponseBody.ListDatasResponseBodyList> list) {
        if (list == null||list.size()==0) {
            return;
        }
        for (ListDatasResponseBody.ListDatasResponseBodyList data : list) {
            ListDatasResponseBody.ListDatasResponseBodyListRaster dataRaster = data.getRaster();
            if (dataRaster != null) {
                DatasetRaster raster = selectDatasetRasterByDataId(data.getDataId());
                boolean isNew = false;
                if(raster==null){
                    isNew = true;
                    raster = new DatasetRaster();
                }
                raster.setDataId(data.getDataId());
                raster.setAcquisitionDateEnd(dataRaster.getAcquisitionDateEnd());
                raster.setAcquisitionDateStart(dataRaster.getAcquisitionDateStart());
                Date date = DateUtils.parseDate(dataRaster.getAcquisitionDateStart());
                if(date==null){
                    date = new Date();
                }
                raster.setAcquisitionDate(DateUtils.parseDateToStr("yyyyMMdd",date));
                List<Double> bbox = dataRaster.getBbox();
                if (bbox != null) {
                    raster.setBboxMinX(bbox.get(0));
                    raster.setBboxMinY(bbox.get(1));
                    raster.setBboxMaxX(bbox.get(2));
                    raster.setBboxMaxY(bbox.get(3));
                }
                raster.setCloudCoverage(dataRaster.getCloudCoverage());
                raster.setName(dataRaster.getName());
                raster.setSourceType(dataRaster.getSourceType());
                raster.setStacId(dataRaster.getStacId());
                raster.setFileDownloadState(DatasetRaster.state_undownload);
                DatasetType datasetType = datasetTypeService.
                        selectDatasetTypeBySourceType(dataRaster.getSourceType());
                if(datasetType!=null){
                    raster.setGroupId(datasetType.getGroupId());
                    raster.setTypeId(datasetType.getId());
                    raster.setWidth(datasetType.getWidth());
                    raster.setHeight(datasetType.getHeight());
                    raster.setBucket(StringUtils.isEmpty(datasetType.getBucket())?"raster":datasetType.getBucket());
                }else {
                    raster.setBucket("raster");
                }
                if(isNew){
                    insertDatasetRaster(raster);
                } else {
                    updateDatasetRaster(raster);
                }
                datasetRasterBandsService.cleanByDataId(data.getDataId());
                List<ListDatasResponseBody.ListDatasResponseBodyListRasterBands> bands = dataRaster.getBands();
                if (bands != null) {
                    for (ListDatasResponseBody.ListDatasResponseBodyListRasterBands band : bands) {
                        DatasetRasterBands bandRaster = new DatasetRasterBands();
                        bandRaster.setRasterId(raster.getId());
                        bandRaster.setDataId(data.getDataId());
                        bandRaster.setBandNo(band.getBandNo());
                        bandRaster.setHeight(band.getHeight()!=null?band.getHeight():band.getWidth());
                        bandRaster.setResolution(band.getResolution());
                        bandRaster.setWidth(bandRaster.getWidth()!=null?bandRaster.getWidth():band.getHeight());
                        datasetRasterBandsService.insertDatasetRasterBands(bandRaster);
                        if(datasetType!=null){
                            DatasetTypeBand typeBand = datasetTypeBandService.
                                    selectDatasetTypeBandTypeAndBandNo(datasetType.getId(),band.getBandNo());
                            if(typeBand==null){
                                typeBand = new DatasetTypeBand();
                                typeBand.setBandName(band.getBandNo());
                                typeBand.setBandKey(band.getBandNo());
                                typeBand.setResolving(band.getResolution());
                                typeBand.setBandNo(band.getBandNo());
                                typeBand.setGroupId(datasetType.getGroupId());
                                typeBand.setTypeId(datasetType.getId());
                                typeBand.setSourceType(datasetType.getSourceType());
                                datasetTypeBandService.insertDatasetTypeBand(typeBand);
                            }else {
                                typeBand.setWidth(bandRaster.getWidth());
                                typeBand.setHeight(bandRaster.getHeight());
                                datasetTypeBandService.updateDatasetTypeBand(typeBand);
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public void updateFileLayerOfRaster(Long rasterId, Long layerId) {
        datasetRasterMapper.updateFileLayerOfRaster(rasterId,layerId);
    }

}
